import { http } from '../services/http';
import type { PaginatedResponse, TemplateListItem } from '../types';

export interface GetTemplatesParams {
    page?: number;
    pageSize?: number;
    category?: string;
    search?: string;
    sort?: string;
}

export const templateApi = {
    // 获取模板列表
    async getTemplates(params: GetTemplatesParams = {}) {
        const { page = 1, pageSize = 12, category = '', search = '', sort = '-updateTime' } = params;
        const response = await http.get<PaginatedResponse<TemplateListItem>>('/templates', {
            params: {
                page,
                pageSize,
                category,
                search,
                sort
            }
        });
        return response.data;
    },

    // 获取单个模板
    async getTemplate(id: string) {
        const response = await http.get(`/templates/${id}`);
        return response.data;
    },

    // 更新模板统计信息
    async updateStats(id: string, type: 'uses' | 'likes' | 'views') {
        const response = await http.post(`/templates/${id}/stats/${type}`);
        return response.data;
    }
};